package main

import (
	"fmt"
	"os/exec"
	"regexp"
	"strings"
)

func main() {
	cmd := exec.Command("lsusb", "-t")
	output, err := cmd.Output()
	lines := strings.Split(string(output), "\n")
	if err != nil {
		fmt.Println("Failed to run lsusb:", err)
		return
	}

	deviceMap := make(map[int]map[string]string)
	currentBus := ""
	port := ""
	dev := ""
	class := ""
	driver := ""
	speed := ""
	var matches []string

	for _, line := range lines {
		line = strings.TrimSpace(line)
		device := make(map[string]string)

		if strings.HasPrefix(line, "/:") {
			re := regexp.MustCompile(`Bus (\d+)\.Port (\d+): Dev (\d+), Class=(\w+), Driver=(\S+), (\d+)M`)
			matches = re.FindStringSubmatch(line)
			bus := matches[1]
			currentBus = bus
			port = matches[2]
			dev = matches[3]
			class = matches[4]
			driver = matches[5]
			speed = matches[6]
		}
		if strings.HasPrefix(line, "|__") {
			re := regexp.MustCompile(`Port (\d+): Dev (\d+), If \d+, Class=([\w\s]+), Driver=([\w/-]+), (\d+\.\d+|\d+)M?`)
			matches = re.FindStringSubmatch(line)
			port = matches[1]
			dev = matches[2]
			class = matches[3]
			driver = matches[4]
			speed = matches[5]
		}

		device["bus"] = currentBus
		device["port"] = port
		device["dev"] = dev
		device["class"] = class
		device["driver"] = driver
		device["speed"] = speed
		deviceMap[len(deviceMap)+1] = device

	}

	fmt.Println("\n")
	for i, device := range deviceMap {
		fmt.Printf("map[%d][bus]=%s\n", i, device["bus"])
		fmt.Printf("map[%d][port]=%s\n", i, device["port"])
		fmt.Printf("map[%d][dev]=%s\n", i, device["dev"])
		fmt.Printf("map[%d][class]=%s\n", i, device["class"])
		fmt.Printf("map[%d][driver]=%s\n", i, device["driver"])
		fmt.Printf("map[%d][speed]=%s\n", i, device["speed"])

		fmt.Println()
	}
}
